package org.itboys.core.service.mysql.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.product.SundaySkuDto;
import org.itboys.core.entity.mysql.SundayProduct;
import org.itboys.core.entity.mysql.SundaySku;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/8/14
 * 描述：
 */
@Service
public class SundaySkuService extends BaseMySqlService<SundaySku>{
    @Autowired
    private SundaySkuPriceService skuPriceService;
    @Autowired
    private SundayProductService productService;
    /**
     * 新增
     * @param sku
     * @throws Exception
     */
    @Transactional
    public void save(
            SundaySku sku
    ) throws  Exception {
        //编辑
        if(LongUtil.isNotZreo(sku.getId())){
            super.update(sku);
        }else{
            super.insert(sku);
            skuPriceService.getPrice(sku.getId(),sku.getInPrice(),sku.getPrice());
        }
    }

    /**
     * 查询
     * @param request
     * @return
     */
    public PageResult<SundaySkuDto> selectSku(HttpServletRequest request,Long productId) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "name","status");
        if(LongUtil.isNotNull(productId)){
            param.put("productId",productId);
        }
        return new PageResult<SundaySkuDto>(packageDtoMuilt(super.select(param)), super.count(param));
    }

    /**
     * 查询详情
     * @param skuId
     * @return
     */
    public SundaySku findOne(Long skuId){
        SundaySku sku=null;
        if(LongUtil.isNotZreo(skuId)){
            sku = super.find(skuId);
        }else {
            sku=new SundaySku();
        }
        return sku;
    }

    /**
     * 上下架
     * @param id
     * @param status
     */
    public void set(Long id,Integer status){
        SundaySku sku = new SundaySku();
        sku.setId(id);
        sku.setStatus(status);
        super.update(sku);

    }

    public List<SundaySkuDto> packageDtoMuilt(List<SundaySku> skus) {
        List<SundaySkuDto> dtos = new ArrayList<SundaySkuDto>();
        List<Long> productIds=new ArrayList<Long>();
        if(ListUtil.isNull(skus)){
            return dtos;
        }
        for(SundaySku sku:skus){
            productIds.add(sku.getProductId());
        }
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("ids",productIds);
        List<SundayProduct> products=productService.select(param);
        for(SundaySku sku:skus){
            String productName="";
            for (SundayProduct product :products) {
                if(product.getId().longValue()==sku.getProductId().longValue()) {
                    productName = product.getName();
                    break;
                }
            }
            dtos.add(new SundaySkuDto(sku.getId(),sku.getCreateTime(),sku.getUpdateTime(),
                    sku.getProductId(),productName, sku.getNumber(),
                    sku.getNetWeight(), sku.getRoughWeight(), sku.getSize(),
                    sku.getLength(),sku.getWidth(),sku.getHeight(),sku.getInPrice(),
                    sku.getPrice(),sku.getStandard(),sku.getStatus(),sku.getSequence()));

        }
        return dtos;
    }
}
